package com.zito.health.util;

import com.zito.health.common.constant.Constant;
import com.zito.health.entity.User;
import com.zito.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: gxt
 * @time: 2019/10/12 10:45
 */
@Component
@Slf4j
public class CacheUtil {

    @Autowired
    private UserService userService;

    private static CacheUtil cacheUtil;

    @PostConstruct
    public void init(){
        cacheUtil = this;
        cacheUtil.userService = this.userService;
    }
    /**
     * 获取SessionUser
     *
     * @return
     */
    public static CacheUser getCacheUser() {

        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        if (null != session) {
            //仅用于测试
            CacheUser cacheUser;//= initCacheUser("front")
            Subject currentUser = SecurityUtils.getSubject();
            cacheUser = (CacheUser) session.getAttribute(Constant.USER_SESSION_KEY);
            if (cacheUser == null) {
                if(currentUser.getPrincipal() != null){
                    cacheUser = CacheUtil.initCacheUser(currentUser.getPrincipal().toString());
                }
            }
            return cacheUser;
        } else {
            log.error("获取用户信息的工具类错啦");
            return null;
        }
        /*CacheUser cacheUser = null;
        if(InvocationInfoProxy.getUserName()!=null){

            User param = new User();
            param.setUsername(InvocationInfoProxy.getUserName());
            User user = cacheUtil.userService.queryOne(param);
            cacheUser = new CacheUser();
            cacheUser.setUser(user);

        }

        return cacheUser;*/

    }
    /*public static CacheUser getAdminUser() {

        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        if (null != session) {
            //仅用于测试
            CacheUser cacheUser;//= initCacheUser("front")
            Subject currentUser = SecurityUtils.getSubject();
            cacheUser = (CacheUser) session.getAttribute(Constant.USER_SESSION_KEY);
            if (cacheUser == null) {
                if(currentUser.getPrincipal() != null){
                    cacheUser = CacheUtil.initCacheUser(currentUser.getPrincipal().toString());
                }
            }
            return cacheUser;
        } else {
            log.error("获取用户信息的工具类错啦");
            return null;
        }

    }*/

    /**
     * 初始化CacheUser对象
     *
     * @param username
     * @return
     */
    public static CacheUser initCacheUser(String username) {

        User param = new User();
        param.setUsername(username);
        User user = cacheUtil.userService.queryOne(param);

        User userSession = new User();
        userSession.setUserid(user.getUserid());
        userSession.setUsername(user.getUsername());
        userSession.setUnitId(user.getUnitId());
        userSession.setRoleId(user.getRoleId());
        userSession.setAreaId(user.getAreaId());
        CacheUser cacheUser = new CacheUser();
        cacheUser.setUser(userSession);

        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        if (null != session) {
            session.setAttribute(Constant.USER_SESSION_KEY, cacheUser);
        } else {
            throw new RuntimeException("CacheUser初始化失败");
        }
        return cacheUser;
    }

    /**
     * 判断用户是否登录
     * @return
     */
    public static boolean isLogin(){
        Subject currentUser = SecurityUtils.getSubject();
        return currentUser.isAuthenticated();
    }
}

